package com.atguigu.gulimail.order.web;

import com.atguigu.gulimail.order.service.OrderService;
import com.atguigu.gulimail.order.vo.OrderConfirmVo;
import com.atguigu.gulimail.order.vo.OrderSubmitVo;
import com.atguigu.gulimail.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;

    @GetMapping("/toTrade")
    public String toTrade(Model model){
       OrderConfirmVo confirmVo =  orderService.confirmOrder();
        model.addAttribute("orderConfirmData", confirmVo);
        return "confirm";
    }

    @PostMapping("submitOrder")
    public String submitOrder( OrderSubmitVo vo,Model model) {

        // 下单：创建订单，校验令牌，锁定库存
       SubmitOrderResponseVo responseVo = orderService.submitOrder(vo);
       if (responseVo.getCode() == 0){
           // 下单成功转去支付页
           model.addAttribute("submitResp",responseVo);
           return "pay";
       }else {
           return "redirect:http://order.gulimall.com/toTrade";
       }
    }


}
